package com.datastructure.test.deleteduplicatelinkedlist;

public class DeleteDuplicatedLinkedList {

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        listNode.next=listNode1;
        listNode1.next=listNode2;
        DeleteDuplicatedLinkedList deleteDuplicatedLinkedList = new DeleteDuplicatedLinkedList();
        ListNode deleteDuplicates = deleteDuplicatedLinkedList.deleteDuplicates(listNode);
        System.out.println(deleteDuplicates);
    }

    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        if(head==null||head.next==null){
            return head;
        }
        int lastValue = Integer.MAX_VALUE;
        ListNode temp = head;
        ListNode lastListNode=head;
        while (head!=null){
            if(head.val==lastValue){
                if(head.next!=null){
                    lastListNode.next=head.next;
                    head=head.next;
                }else {
                    lastListNode.next=null;
                    head=head.next;
                }
            }else {
                lastListNode=head;
                lastValue = head.val;
                head=head.next;
            }
        }
        return temp;
    }
}

class ListNode {
   int val;
   ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}
